using UnityEngine;
using QFramework;
using System;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{

    public partial class SimpleSword : ViewController
    {

        float float_Time = 0;
        void Start()
        {
            // Code Here
        }

        void Update()
        {
            float_Time += Time.deltaTime;

            if (float_Time >= Global.SimpleAbilityDuration.Value)
            {
                float_Time = 0;

                var miniEnemys = FindObjectsByType<MiniMenemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
                foreach (var item in miniEnemys)
                {
                    float dis = (Player.Default.transform.position - item.transform.position).magnitude;
                    if (dis <= 3)
                    {
                        // Enemy tempEnemty = item;
                        // tempEnemty.OnSetHp(-Global.SimpleAbilityAtk.Value);
                        OnHitEnemyAnim(item.Position());

                    }
                }

                var enemys = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
                foreach (var item in enemys)
                {
                    float dis = (Player.Default.transform.position - item.transform.position).magnitude;
                    if (dis <= 3)
                    {
                        // Enemy tempEnemty = item;
                        // tempEnemty.OnSetHp(-Global.SimpleAbilityAtk.Value);
                        OnHitEnemyAnim(item.Position());

                    }
                }
            }
        }

        /// <summary>
        /// 实例劈砍小刀
        /// </summary>
        /// <param name="enemyPos">敌人位置</param>
        private void OnHitEnemyAnim(Vector3 enemyPos)
        {
            Sword.Instantiate()
                       .Position(enemyPos + Vector3.left * 0.25f)
                       .Self((self) =>
                       {
                           var selfCache = self;
                           selfCache.OnTriggerEnter2DEvent((collection) =>
                           {
                               if (collection == null || collection.GetComponent<HurttBox>() == null || collection.GetComponent<HurttBox>().owner == null) return;

                               if (collection.GetComponent<HurttBox>().owner.CompareTag("Enemy"))
                               {
                                   collection.GetComponent<HurttBox>().owner.GetComponent<IEnemy>().OnSetHp(-Global.SimpleAbilityAtk.Value);
                               }

                           }).UnRegisterWhenGameObjectDestroyed(selfCache);

                           //劈砍动画
                           ActionKit.Sequence()
                           .Callback(() => { selfCache.enabled = false; })
                           .Parallel(p =>
                           {
                               p.Lerp(0, 10, 0.2f, (z) =>
                               {
                                   selfCache.transform.localEulerAngles = selfCache.transform.localEulerAngles.Z(z);
                               });

                               p.Append(ActionKit.Sequence()
                               .Lerp(0, 1.25f, 0.1f, scale => selfCache.LocalScale(scale)))
                               .Lerp(1.25f, 1f, 0.1f, scale => selfCache.LocalScale(scale));
                           })
                       .Callback(() =>
                       {
                           selfCache.enabled = true;
                       })
                       .Parallel((p) =>
                       {
                           p.Lerp(10, -180, 0.2f, (z) =>
                           {
                               selfCache.transform.localEulerAngles = self.transform.localEulerAngles.Z(z);
                               p.Append(ActionKit.Sequence()
                          .Lerp(1, 1.25f, 0.1f, scale => selfCache.LocalScale(scale)))
                          .Lerp(1.25f, 1f, 0.1f, scale => selfCache.LocalScale(scale));
                           });
                       })
                       .Callback(() => selfCache.enabled = false)
                       .Lerp(-180, 0, 0.3f, z =>
                       {
                           selfCache.transform.localEulerAngles = selfCache.transform.localEulerAngles.Z(z);
                           selfCache.LocalScale(z.Abs() / 180);
                       })
                       .Start(this, () => { self.DestroySelfGracefully(); });
                       })
                       .Show();
        }
    }
}
